package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    //根据品牌id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable Long id) {
        Brand brand = brandService.getById(id);
        BrandDTO brandDTO = new BrandDTO(brand);
        return ResponseEntity.ok(brandDTO);
    }

    //根据品牌id集合查询品牌集合
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        List<Brand> brandList = brandService.listByIds(ids);
        List<BrandDTO> brandDTOList = BrandDTO.convertEntityList(brandList);
        return ResponseEntity.ok(brandDTOList);
    }

    //分页查询品牌
    @GetMapping("/page")
    //key搜索关键字 page当前页码 rows每页大小
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(@RequestParam(value = "key",required = false) String key,
                                                              @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                              @RequestParam(value = "rows",defaultValue = "5") Integer rows){
        PageDTO<BrandDTO> pageDTO = brandService.queryBrandByPage(key,page,rows);
        return ResponseEntity.ok(pageDTO);
    }

    //根据分类id查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long id){
        List<BrandDTO> brandDTOList = brandService.queryBrandByCategory(id);
        return ResponseEntity.ok(brandDTOList);
    }

    //新增品牌
    @PostMapping
    //Void 无返回值
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        // 新增成功，无返回值，返回201 build()
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //更新品牌
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据ID删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id){
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
